package com.project.controller.shopController;

import com.project.bean.GoodsBean;
import com.project.bean.ShopBean;
import com.project.service.goodsService.IGoodsService;
import com.project.service.shopService.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;
    @Autowired
    private IGoodsService goodsService;

    @RequestMapping("/findAll")
    public List<ShopBean> findAll(HttpServletRequest request){
        Map<Integer, ShopBean> shopMap = (Map<Integer, ShopBean>) request.getSession().getAttribute("ShopMap");
        if(shopMap.size() == 0){
            return null;
        }
        List<ShopBean> shopList = new ArrayList<>(shopMap.values());
        return shopList;
    }

    @RequestMapping("/add")
    public String add(int goodsId, HttpServletRequest request, int num){
        Map<Integer, ShopBean> shopMap = (Map<Integer, ShopBean>) request.getSession().getAttribute("ShopMap");
        GoodsBean goodsBean = goodsService.findById(goodsId);
        shopService.add(goodsBean, shopMap, num);
        return "ok";
    }

    @RequestMapping("/del")
    public String del(int goodsId, HttpServletRequest request){
        Map<Integer, ShopBean> shopMap = (Map<Integer, ShopBean>) request.getSession().getAttribute("ShopMap");
        shopService.del(goodsId, shopMap);
        return "ok";
    }

    @RequestMapping("/update")
    public String update(int goodsId, HttpServletRequest request, Integer number) {
        Map<Integer, ShopBean> shopMap = (Map<Integer, ShopBean>) request.getSession().getAttribute("ShopMap");
        shopService.update(goodsId, shopMap, number);
        return "ok";
    }

}
